﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Diagnostics;

namespace WpfDemo.Event
{
    /// <summary>
    /// ButtonEventWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ButtonEventWindow : Window
    {
        public static readonly RoutedCommand OpenCommand = new RoutedCommand("Open", typeof(ButtonEventWindow));
        public static readonly RoutedCommand BlockCommand = new RoutedCommand("Block", typeof(ButtonEventWindow));

        public ButtonEventWindow()
        {
            InitializeComponent();

            FileInfo[] fileList = new DirectoryInfo("c:\\").GetFiles("*.*");
            lstFile.ItemsSource = fileList;


            CommandBindings.Add(new CommandBinding(OpenCommand, (s, args) =>
                {
                    Process.Start("notepad.exe", (String)args.Parameter);
                }));
            CommandBindings.Add(new CommandBinding(OpenCommand, (s, args) =>
            {
                MessageBox.Show((string)args.Parameter, "Block");
            }));
        }

        private void mnuTest1_Click(object sender, RoutedEventArgs e)
        {
        }

        private void btnExam1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("sss");
        }

        private void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //e.Handled = true;
        }
    }

    public class FileToCommandConvert : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string ext = ((FileInfo)value).Extension.ToLowerInvariant();
            if (ext == ".txt")
            {
                return ButtonEventWindow.OpenCommand;
            }
            else
            {
                return ButtonEventWindow.BlockCommand;
            }
            
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
